package rin.android.gamble;


import rin.android.database.ClassPlayer;
import rin.android.database.Database;
import rin.android.sound.MusicBackground;
import rin.android.sound.Sound;


import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends MyApp implements OnClickListener {
	Database mData;
	ClassPlayer mPlayer;
	TextView playerName;
	TextView playerDollar;
	MusicBackground mMusic;
	public static Sound mSound;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu);
		mMusic = new MusicBackground();
		mMusic.loadMusic(this);
		if (Setting.isMusic)
            mMusic.play();
        else{
            mMusic.play();
            mMusic.pause();
        }
		mSound = new Sound();
        mSound.loadSound(this);
		Database mData = new Database(this);
        mData.openDatabase();
		//Nhận gói dữ liệu từ Intent gửi đến
        Bundle extras = getIntent().getExtras();
       
        //Nếu không có dữ liệu thì không làm gì nữa
        if (extras == null) {
            return;
        }
        //Nếu có dữ liệu thì lấy đưa vào chuỗi với key tương ứng
        String userName = extras.getString("userName");
        //
        
        mPlayer = mData.getPlayerByName(userName);
        playerName = (TextView)findViewById(R.id.username);
        playerDollar = (TextView)findViewById(R.id.score);
        playerName.setText("Player: "+mPlayer.getName());
        playerDollar.setText("$$$: "+mPlayer.getDollar()+"");
        mData.closeDatabase();
        
		View taiXiuBtn = findViewById(R.id.bttaixiu);
		taiXiuBtn.setOnClickListener(this);
		View baiCaoBtn = findViewById(R.id.btbaicao);
		baiCaoBtn.setOnClickListener(this);
		View heightScoreButton = findViewById(R.id.btop10);
		heightScoreButton.setOnClickListener(this);
		View aboutButton = findViewById(R.id.btabout);
		aboutButton.setOnClickListener(this);
		View settingButton = findViewById(R.id.setting);
		settingButton.setOnClickListener(this);
		View exitButton = findViewById(R.id.btexit);
		exitButton.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	switch (item.getItemId()) {
	case R.id.settings:
	startActivity(new Intent(this, MySharedPreferences.class));
	return true;
	// More items go here (if any) ...
	}
	return false;
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent i;
		mSound.playClick();
		switch (v.getId()) {
		case R.id.bttaixiu:
			i = new Intent(this, TaiXiu.class);
			i.putExtra("userName", mPlayer.getName());
			i.putExtra("dollar", mPlayer.getDollar()+"");
			finish();
			startActivity(i);
			break;
		case R.id.btbaicao:
			i = new Intent(this, BaiCao.class);
			i.putExtra("userName", mPlayer.getName());
			i.putExtra("dollar", mPlayer.getDollar()+"");
			finish();
			startActivity(i);
			break;
		case R.id.btop10:
			i = new Intent(this, HeightDollar.class);
			//onPause();
			startActivity(i);
			break;
		case R.id.btabout:
			i = new Intent(this, About.class);
			startActivity(i);
			break;
		case R.id.setting:
			i = new Intent(this, Setting.class);
			startActivity(i);
			break;
		case R.id.btexit:
			finish();
			break;
		}
	}
	@Override
	public void onDestroy(){
		super.onDestroy();
		mMusic.release();
	}
	@Override
	public void onPause(){
		super.onPause();
		mMusic.pause();
	}
	@Override
	public void onResume(){
		super.onResume();
		if(Setting.isMusic)
		mMusic.resume();
	}
}
